home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / ABox 1.9.5 / CPlus Files / ABSlide.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-26  |  15.9 KB  |  680 lines  |  [TEXT/MMCC]

  1. /*    
  2.     Copyright © 1991-1995 by TopSoft Inc.  All rights reserved.
  3.  
  4.     You may distribute this file under the terms of the TopSoft
  5.     Artistic License, accompanying this package.
  6.     
  7.     This file was developed by George (ty) Tempel in connection with TopSoft, Inc..
  8.     See the Modification History for more details.
  9.  
  10. Product
  11.     About Box
  12.  
  13. FILE
  14.     ABSlide.c
  15.  
  16. NAME
  17.     ABSlide.c, part of the ABox project source code,
  18.     responsible for handling the AboutBox slide class stuff.
  19.  
  20. DESCRIPTION
  21.     This file contains defines for the about box modules.
  22.     
  23. DEVELOPED BY
  24.     George (ty) Tempel                ttempel@monmouth.com
  25.     All code in this file, and its associated header file was
  26.     Created by George (ty) Tempel in connection with the TopSoft, Inc.
  27.     "FilterTop" application development, except where noted.
  28.  
  29. CARETAKER - George (ty) Tempel <ttempel@monmouth.com>
  30.      Please consult this person for any changes or suggestions to this file.
  31.  
  32. MODIFICATION HISTORY
  33.  
  34.     dd mmm yy    -    xxx    -    patchxx: description of patch
  35.     9 June 94    -    ty    -    Initial Version Created
  36.     20-july-94    -    ty    -    initial version released
  37.     26-apr-95    -    ty    -    1.0.7--fixed handling of return type from
  38.                                 res->Event() within ForEach()
  39.     23-may-95    -    ty    -    changes for compatibility with the CodeWarrior CW6
  40.                             release and the associated Universal Headers from Apple:
  41.                             most methods that returned references now have "Ref" at
  42.                             the end of their methods names to prevent possible collisions
  43.                             with datatypes and classes of the same name (older versions
  44.                             of the compiler didn't have a problem with this).
  45.     25-oct-95    -    ty    -    changes for "const" usage under CW7; simplification of Boolean
  46.                             query methods
  47.  
  48. */
  49.  
  50. /*===========================================================================*/
  51.  
  52. /*======= Segmentation directives ========*/
  53.  
  54. #ifdef USE_MANUAL_SEGMENTATION
  55. #pragma segment ty
  56. #endif
  57.  
  58. /*============ Header files ==============*/
  59.     
  60. #include     "ABSlide.h"
  61. #include    "ABResource.h"
  62.  
  63. #include    "ABPict.h"
  64. #include    "ABText.h"
  65. #include    "ABStr.h"
  66. #include    "ABSound.h"
  67.  
  68.  
  69. /*=============== Globals ================*/
  70.  
  71. /*================ CODE ==================*/
  72.  
  73.  
  74. /*=============================== ABSlide::ABSlide ================================*/
  75. ABSlide::ABSlide(void)
  76. {
  77.     this->Resources() = new ABLinkedList;
  78.     this->SlideNumber() = 0;
  79.     this->Initialized() = false;
  80. } // end ABSlide
  81.  
  82.  
  83.  
  84. /*=============================== ABSlide::~ABSlide ================================*/
  85. ABSlide::~ABSlide(void)
  86. {
  87.     this->ResetResources();
  88.  
  89. } // end ~ABSlide
  90.  
  91.  
  92.  
  93. /*=============================== ABSlide::ResetResources ================================*/
  94. void
  95. ABSlide::ResetResources(void)
  96. {
  97.     if (this->HasResources())
  98.     {
  99.         delete this->Resources();
  100.         this->Resources() = NULL;
  101.     }
  102. } // end ResetResources
  103.  
  104.  
  105.  
  106. /*=============================== ABSlide::GetProperty ================================*/
  107. OSErr    ABSlide::GetProperty(ABProperty prop, 
  108.                                 void *ptr, 
  109.                                 long *ptrSize)
  110. {
  111.     OSErr    error = noErr;
  112.     long    pSize;
  113.     
  114.     //    begin here...
  115.     
  116.     if (!ptr)
  117.         return kABPropertyNullStorage;
  118.     
  119.     switch (prop)
  120.     {
  121.         case    kABSlideNumber:
  122.                     (*(short *)ptr) = this->GetSlideNumber();
  123.                     pSize = kABSlideNumberSize;
  124.                     break;
  125.         case    kABSlideIndex:
  126.                     (*(ABIndex *)ptr) = this->Ordinal();
  127.                     pSize = kABSlideIndexSize;
  128.                     break;
  129.         case    kABSlideNumberOfElements:
  130.                     if (this->HasResources())
  131.                         (*(ABListCount *)ptr) = this->Resources()->Count();
  132.                     else
  133.                         (*(ABListCount *)ptr) = 0;
  134.                     pSize = kABSlideNumberOfElementsSize;
  135.         default:
  136.                     error = kABSlideSuperProperties::GetProperty(prop, ptr, ptrSize);
  137.                     break;
  138.     } // end switch block
  139.     
  140.     if (ptrSize && !error)
  141.         *ptrSize = pSize;
  142.     return error;
  143.     
  144. } // end GetProperty
  145.  
  146.  
  147.  
  148. /*=============================== ABSlide::SetProperty ================================*/
  149. OSErr    ABSlide::SetProperty(ABProperty prop, 
  150.                                 void *ptr, 
  151.                                 long ptrSize)
  152. {
  153.     OSErr    error = noErr;
  154.     
  155.     //    begin here...
  156.     
  157.     if (!ptr)
  158.         return kABPropertyNullStorage;
  159.     
  160.     switch (prop)
  161.     {
  162.         case    kABSlideNumber:
  163.                     this->SlideNumber() = (*(short *)ptr);
  164.                     break;
  165.         case    kABSlideNumberOfElements:
  166.                     error = kABPropertyReadOnly;
  167.                     break;
  168.         default:
  169.                     error = kABSlideSuperProperties::SetProperty(prop, ptr, ptrSize);
  170.                     break;
  171.     } // end switch block
  172.     
  173.     return error;
  174.     
  175. } // end SetProperty
  176.  
  177.  
  178.  
  179.  
  180.  
  181. /*=============================== ABSlide::InitializeObject ================================*/
  182. OSErr    ABSlide::InitializeObject(void)
  183. {
  184.     OSErr        error = noErr;
  185.     
  186.     ABResource    *res;
  187.     short        resID = this->GetSlideNumber();
  188.     
  189.     //    begin here...
  190.     //
  191.     //    attempt to load each of the resources we'd
  192.     //    be interested in for the slide. The caller may
  193.     //    have already added some to our list, so we'll
  194.     //    concern ourselves with appending to the list.
  195.     //
  196.     if (this->IsInitialized())
  197.         return noErr;
  198.  
  199.     res = new ABPict;
  200.     if (res)
  201.     {
  202.         error = res->SetProperty (kABResourceResID, &resID, kABResourceResIDSize);
  203.         if (res->CheckFile(NULL))
  204.         {
  205.             error = this->AddResource(res);
  206.         } else {
  207.             delete res;
  208.         } // end if block
  209.     } // end if block
  210.     
  211.     
  212.     res = new ABText;
  213.     if (res)
  214.     {
  215.         error = res->SetProperty (kABResourceResID, &resID, kABResourceResIDSize);
  216.         if (res->CheckFile(NULL))
  217.         {
  218.             error = this->AddResource(res);
  219.         } else {
  220.             delete res;
  221.         } // end if block
  222.     } // end if block
  223.     
  224.     
  225.     res = new ABSound;
  226.     if (res)
  227.     {
  228.         error = res->SetProperty (kABResourceResID, &resID, kABResourceResIDSize);
  229.         if (res->CheckFile(NULL))
  230.         {
  231.             error = this->AddResource(res);
  232.         } else {
  233.             delete res;
  234.         } // end if block
  235.     } // end if block
  236.     
  237.     
  238.     res = new ABStr;
  239.     if (res)
  240.     {
  241.         error = res->SetProperty (kABResourceResID, &resID, kABResourceResIDSize);
  242.         if (res->CheckFile(NULL))
  243.         {
  244.             error = this->AddResource(res);
  245.         } else {
  246.             delete res;
  247.         } // end if block
  248.     } // end if block
  249.     
  250.     this->Initialized() = true;
  251.     
  252.     return error;
  253.     
  254. } // end InitializeObject
  255.  
  256.  
  257.  
  258.  
  259. /*=============================== ABSlide::ForEach ================================*/
  260. //
  261. //    ForEach is an internal method to do some action/method for each
  262. //    list member.
  263. //
  264. OSErr    ABSlide::ForEach (ABMessage message, void *data)
  265. {
  266.     OSErr        error = noErr;
  267.     long        i;
  268.     long        limit;
  269.     ABResource    *res = NULL;
  270.     
  271.     //    begin here...
  272.     
  273.     if (!this->IsInitialized() && (message != kABMessageStop))
  274.         error = this->InitializeObject();
  275.     
  276.     if (this->HasResources() && !error)
  277.         limit = this->Resources()->Count();
  278.     else
  279.         return error;
  280.     
  281.     if (limit < 1)
  282.     {
  283.         error = noErr;
  284.     } else {
  285.         for (i = 1; i <= limit; ++i)
  286.         {
  287.             Boolean handled = false;
  288.             
  289.             res = (ABResource *)(this->Resources()->NthLink(i));
  290.             if (res)
  291.             {
  292.                 switch (message)
  293.                 {
  294.                     case    kABMessageDraw:
  295.                             error = res->Draw((WindowPtr)data);
  296.                             break;
  297.                     case    kABMessageUpdate:
  298.                             error = res->Update((WindowPtr)data);
  299.                             break;
  300.                     case    kABMessageEvent:
  301.                             handled = res->Event((EventRecord *)data);
  302.                             break;
  303.                     case    kABMessageStop:
  304.                             error = res->Stop();
  305.                             break;
  306.                 } // end switch block
  307.             } else {
  308.                 //    couldn't locate the ith resource for this slide! yikes!
  309.                 //
  310.                 res = NULL;
  311.             } // end if block
  312.         } // end for loop
  313.  
  314.     }
  315.     return error;
  316.     
  317. } // end ForEach()
  318.  
  319.  
  320.  
  321.  
  322. /*=============================== ABSlide::Draw ================================*/
  323. OSErr    ABSlide::Draw(WindowPtr window)
  324. {
  325.     return this->ForEach (kABMessageDraw, window);    
  326. } // end Draw
  327.  
  328.  
  329.  
  330.  
  331.  
  332. /*=============================== ABSlide::AddResource ================================*/
  333. OSErr    ABSlide::AddResource(ABResource *obj)
  334. {
  335.     OSErr        error = noErr;
  336.     short        resID = this->GetSlideNumber();
  337.     
  338.     //    begin here...
  339.     //
  340.     //    the intent here is to add the given object to the
  341.     //    list maintained by the slide. The object will be
  342.     //    given the same resource ID# as the slide.
  343.     //
  344.     if (obj && this->HasResources())
  345.     {
  346.         error = obj->SetProperty (kABResourceResID, &resID, kABSlideNumberSize);
  347.         this->Resources()->Append ((ABLink *)obj);
  348.     } else {
  349.         error = paramErr;
  350.     } // end if block
  351.     
  352.     return error;
  353.     
  354. } // end AddResource
  355.  
  356.  
  357.  
  358.  
  359.  
  360. /*=============================== ABSlide::DropResource ================================*/
  361. OSErr    ABSlide::DropResource(ABResource *obj)
  362. {
  363.     OSErr        error = noErr;
  364.  
  365.     //    begin here...
  366.     //
  367.     //    the intent here is to delete the given object to the
  368.     //    list maintained by the slide.
  369.     //
  370.     if (obj && this->HasResources())
  371.     {
  372.         this->Resources()->Detach ((ABLink *)(obj));
  373.     } else {
  374.         error = paramErr;
  375.     } // end if else block
  376.     
  377.     return error;
  378.     
  379. } // end DropResource
  380.  
  381.  
  382.  
  383.  
  384.  
  385. /*=============================== ABSlide::Update ================================*/
  386. OSErr    ABSlide::Update(WindowPtr window)
  387. {
  388.     return this->ForEach (kABMessageUpdate, window);    
  389. } // end Update
  390.  
  391.  
  392.  
  393.  
  394. /*=============================== ABSlide::Event ================================*/
  395. Boolean    ABSlide::Event(EventRecord *eventRec)
  396. {
  397.     return (Boolean)this->ForEach (kABMessageEvent, eventRec);    
  398.     
  399. } // end Event
  400.  
  401.  
  402.  
  403. /*=============================== ABSlide::Stop ================================*/
  404. OSErr    ABSlide::Stop(void)
  405. {
  406.     return this->ForEach (kABMessageStop, NULL);    
  407. } // end Stop
  408.  
  409.  
  410.  
  411.  
  412. /*=============================== ABSlide::GetResourceOfType ================================*/
  413. ABResource    *ABSlide::GetResourceOfType(ResType type)
  414. {
  415.     ABResource    *rsrc;
  416.     ABIndex        i;
  417.     ABListCount    limit;
  418.     ResType        resType;
  419.     long        size = kABResourceResTypeSize;
  420.     OSErr        error = noErr;
  421.     
  422.     //    begin here...
  423.     if (!this->IsInitialized())
  424.         error = this->InitializeObject();
  425.     
  426.     
  427.     if (this->HasResources())
  428.         limit = this->Resources()->Count();
  429.     else
  430.         return NULL;
  431.     
  432.     for (i = 1; i <= limit; ++i)
  433.     {
  434.         rsrc = (ABResource *)(this->Resources()->NthLink(i));
  435.         if (!rsrc)
  436.             continue;
  437.         error = rsrc->GetProperty(kABResourceResType, &resType, &size);
  438.         if (!error && (resType == type))
  439.             return rsrc;
  440.     } // end for loop
  441.     
  442.     return NULL;
  443.     
  444. } // end GetResourceOfType
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451. /*=============================== ABSlide::Resize ===============================*/
  452. //
  453. //    this function will rearrange and resize the picture and the
  454. //    text fields that appear on a slide.
  455. //
  456. //    10-nov-93    -    ty    -    d25 changes to improve picture
  457. //                            placement
  458. //
  459. //    20-nov-93    -    ty    -    d26 changes to support the unification
  460. //                            of the slideField with the slideTitleField
  461. //                            to reduce the number of things on the screen
  462. //                            and to provide more room for slides if no
  463. //                            title's are present.
  464. //
  465. //    is called by:
  466. //
  467. OSErr    ABSlide::Resize (Rect const *field)
  468. {
  469.     OSErr                    error = noErr;
  470.  
  471.     ABResource                *pict;
  472.     ABResource                *text;
  473.     ABResource                *title;
  474.     
  475.     Rect                    area;
  476.     Rect                    pictRect;
  477.     Rect                    textRect;
  478.     Rect                    titleRect;
  479.                 
  480.     short                    fieldWidth,
  481.                             fieldHeight;
  482.     
  483.     short                    offsetX,
  484.                             offsetY;
  485.     
  486.     SlideElementPositions    position = leftOrRight;
  487.     
  488.     PicHandle                pictureH;
  489.     
  490.     short                    fontHeight;            //    1.0a9p8 ty...added
  491.     short                    maximumTitleHeight;    //    1.0a9p8 ty...added
  492.     
  493.     
  494.     //    begin here...    
  495.  
  496.     if (!this->IsInitialized())
  497.         error = this->InitializeObject();
  498.     
  499.     if (!this->HasResources())
  500.         return paramErr;
  501.     
  502.     if (!field)
  503.         return paramErr;
  504.     else
  505.         area = *field;
  506.         
  507.     //    erase the field...
  508.     error = this->EraseFrame(area);
  509.     
  510.     //    calculation of fontHeight and maximumTitleHeight for better box sizing
  511.     fontHeight = ABUFonts::FindFontHeight();
  512.     maximumTitleHeight = 2 * fontHeight;
  513.     
  514.     //    get the slide elements we care about
  515.     pict = this->GetResourceOfType(kABPictResource);
  516.     text = this->GetResourceOfType(kABTextResource);
  517.     title = this->GetResourceOfType(kABStringResource);
  518.  
  519.     //    check to see if we should even bother
  520.     if (!(pict || text || title))
  521.         return error;
  522.  
  523.     titleRect = pictRect = textRect = area;
  524.     if (title)
  525.     {
  526.         titleRect.top = titleRect.bottom - maximumTitleHeight;
  527.         area.bottom = pictRect.bottom = textRect.bottom = titleRect.top - 2*kABdialogSpacing;
  528.     } // end if block
  529.     
  530.     //    now save the modified sub areas into the proper
  531.     //    slide elements
  532.     if (pict)
  533.         error = pict->SetProperty(kABObjectRect, &pictRect, kABObjectRectSize);
  534.     if (text)
  535.         error = text->SetProperty(kABObjectRect, &textRect, kABObjectRectSize);
  536.     if (title)
  537.         error = title->SetProperty(kABObjectRect, &titleRect, kABObjectRectSize);
  538.     
  539.     //    now see if things need to be rearranged
  540.     if (pict) 
  541.     {
  542.         //    now get the picture information
  543.         error = pict->GetProperty(kABResourceHandle, &pictureH, NULL);
  544.         
  545.         if (pictureH && !error) 
  546.         {
  547.             //    1.0a7 ty...the two following statements were rewritten to utilize (*pictureH)->picFrame
  548.             //                rather than the information from GetPictInfo.
  549.             //
  550.             ::HLock ((Handle)pictureH);
  551.             pictRect.right = pictRect.left + ((*pictureH)->picFrame.right - (*pictureH)->picFrame.left);
  552.             pictRect.bottom = pictRect.top + ((*pictureH)->picFrame.bottom - (*pictureH)->picFrame.top);
  553.             ::HUnlock ((Handle)pictureH);
  554.             
  555.             //    decide what the aspect ratio is and 
  556.             //    where it should fit in the field.
  557.             //
  558.             if ((pictRect.right - pictRect.left) < (pictRect.bottom - pictRect.top)) 
  559.             {
  560.                 position = leftOrRight;
  561.             } else {
  562.                 position = aboveOrBelow;
  563.             }    //    end if else block
  564.         
  565.         } else {
  566.             //    there is a problem with the handle,
  567.             //    so the picture element is basically useless to us.
  568.             pict = NULL;
  569.             pictRect.right = pictRect.top = pictRect.left = pictRect.bottom = 0;
  570.         } // end if block
  571.     } // end if (pict)
  572.     
  573.     //    if the picture is smaller than our field then we'll just center
  574.     //    and display the picture; if the picture is larger than the field
  575.     //    we'll scale it.
  576.     //
  577.     
  578.     fieldWidth = area.right - area.left;
  579.     fieldHeight = area.bottom - area.top;
  580.     
  581.     //    if there are both text and pict objects then we
  582.     //    need to arrange them properly on the field. If there
  583.     //    is only a pict, then it will be centered in the field.
  584.     //    If there is only a text, then it already has its field
  585.     // 
  586.     if (text && pict) 
  587.     {
  588.         if (position == aboveOrBelow) 
  589.         {
  590.             //Rect    box;
  591.             
  592.             //    make enough room for the text area below...no additional
  593.             //    reduction is necessary
  594.             
  595.             //box = *field;
  596.             //box.bottom -= kABscrollBarMinLength;
  597.             //
  598.             //    addition of the scroll-bar area to the right
  599.             //    side to allow the pict to occupy the entire area
  600.             //    left to right, and move the bottom up to allow for
  601.             //    the minimum size text field possible.
  602.             //
  603.             area.right += kABscrollBarWidth;
  604.             area.bottom -= kABscrollBarMinLength;
  605.             error = this->ScaleRectToFit (pictRect, area, 1.0);
  606.             //    restore the field geometry
  607.             area.bottom += kABscrollBarMinLength;
  608.             area.right -= kABscrollBarWidth;
  609.             
  610.             //    use the space below the picture as the text rect
  611.             //    and center the picture left/right
  612.             //
  613.             offsetX = (fieldWidth - (pictRect.right - pictRect.left))/2;
  614.             offsetY = 0;
  615.     
  616.         } else {
  617.             error = this->ScaleRectToFit (pictRect, area, kABreduceFactor);
  618.             //    use the space to the right of the picture as the textRect
  619.             //    and center the graphic top/bottom on the left.
  620.             //
  621.             offsetX = 0;
  622.             offsetY = (fieldHeight - (pictRect.bottom - pictRect.top))/2;
  623.  
  624.         }    //    end if block
  625.     } else if (pict && !text) {
  626.         //    addition of the scroll-bar area to the right
  627.         //    side to allow the pict to occupy the entire area,
  628.         //    both left to right and top to bottom
  629.         //
  630.         pictRect.right += kABscrollBarWidth;
  631.         area.right += kABscrollBarWidth;
  632.         error = this->ScaleRectToFit (pictRect, area, 1.0);
  633.         area.right -= kABscrollBarWidth;
  634.         
  635.         //    there is only a graphic, so we should center the graphic top/bottom 
  636.         //    and left/right in the field.
  637.         //
  638.         offsetX = (fieldWidth - (pictRect.right - pictRect.left))/2;
  639.         offsetY = (fieldHeight - (pictRect.bottom - pictRect.top))/2;
  640.         
  641.     } else if (text && !pict) {
  642.         //    there is only a text field, so let it occupy the whole field
  643.         //
  644.         offsetX = offsetY = 0;
  645.     
  646.     } // end if....elseif block
  647.     
  648.  
  649.     //    move the picture rect...
  650.     //
  651.     if (pict)
  652.         ::OffsetRect (&pictRect, offsetX, offsetY);
  653.  
  654.     //    and now size the text rect accordingly...
  655.     if (text)
  656.     {
  657.         if (offsetX > 0)
  658.             textRect.top = pictRect.bottom + kABdialogSpacing;
  659.         if (offsetY > 0)
  660.             textRect.left = pictRect.right + kABdialogSpacing;
  661.         error = text->SetProperty(kABObjectRect, &textRect, kABObjectRectSize);
  662.     } // end if block
  663.     
  664.     //    now save the modified sub areas into the proper
  665.     //    slide elements
  666.     if (pict)
  667.         error = pict->SetProperty(kABObjectRect, &pictRect, kABObjectRectSize);
  668.     if (title)
  669.         error = title->SetProperty(kABObjectRect, &titleRect, kABObjectRectSize);
  670.     
  671.     //    we're done!
  672.     return error;
  673.     
  674. }    //    end of function Resize()
  675.  
  676.  
  677.  
  678. //    end of file
  679.  
  680.